package com.faf.ui;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import com.faf.bean.Food;
import com.faf.service.FoodService;
import com.faf.service.impl.FoodServiceImpl;

/**
 * Servlet implementation class UpdateFood
 */
@WebServlet("/UpdateFoodManagement")
public class UpdateFoodManagement extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public UpdateFoodManagement() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		response.getWriter().append("Served at: ").append(request.getContextPath());
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub

		response.setContentType("text/html");
		response.setCharacterEncoding("utf-8");
		PrintWriter out = response.getWriter();
		request.setCharacterEncoding("utf-8");
		Food food = new Food();
		String foodId = null, foodName = null, foodPrice = null, foodType = null, foodDescribe = null,
				foodPhoto1 = null, foodPhoto = null;
		boolean ismultipart = ServletFileUpload.isMultipartContent(request);
		if (ismultipart) {
			try {
				FileItemFactory factory = new DiskFileItemFactory();
				ServletFileUpload upload = new ServletFileUpload(factory);
				List<FileItem> items = upload.parseRequest(request);
				for (FileItem fileItem : items) {
					if (fileItem.isFormField()) {
						String name = fileItem.getFieldName();
						String value = fileItem.getString();
						if ("foodId".equals(name)) {
							foodId = new String(value.getBytes("iso-8859-1"), "utf-8");
							food.setFoodId(Integer.parseInt(foodId));
						} else if ("foodName".equals(name)) {
							foodName = new String(value.getBytes("iso-8859-1"), "utf-8");
							food.setFoodName(foodName);
						} else if ("foodPrice".equals(name)) {
							foodPrice = new String(value.getBytes("iso-8859-1"), "utf-8");
							food.setFoodPrice(Double.parseDouble(foodPrice));
						} else if ("foodType".equals(name)) {
							foodType = new String(value.getBytes("iso-8859-1"), "utf-8");
							food.setFoodType(foodType);
						} else if ("foodDescribe".equals(name)) {
							System.out.println(value);
							foodDescribe = new String(value.getBytes("iso-8859-1"), "utf-8");
							food.setFoodDescribe(foodDescribe);
						} else if ("foodPhoto1".equals(name)) {
							System.out.println(value);
							foodPhoto1 = new String(value.getBytes("iso-8859-1"), "utf-8");
							food.setFoodPhoto(foodPhoto1);
						}
					} else {
						// 获取servlet的访问全路径--http: / /Localhost:8080/daye7_war_explode

						String url = getServletContext().getRealPath("/") + "Image/goods/";

						Date date = new Date();
						SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
						String fileName = "/" + formatter.format(date) + ".jpg";// 用日期当作名字
						File file = new File(url);
						if (!file.exists()) {
							file.mkdirs();
						}
						File savefile = new File(file, fileName);
						System.out.println(savefile);// 路径
						System.out.println(savefile.getName());// 名字
						foodPhoto = "Image/goods/" + savefile.getName();
						System.out.println(foodPhoto);
						if (fileItem.getSize() > 0) {
							InputStream in = fileItem.getInputStream();
							OutputStream os = new FileOutputStream(savefile);
							int len;
							byte[] b = new byte[1024];
							while ((len = in.read(b)) != -1) {
								os.write(b, 0, len);
							}
							food.setFoodPhoto(foodPhoto);
							os.close();
							in.close();
						} else {
							food.setFoodPhoto(foodPhoto1);
						}
					}
				}
				FoodService foodService = new FoodServiceImpl();
				foodService.updateFood(food);
				response.sendRedirect("../FAFCode/FoodManagement/manageFood.jsp");
			} catch (Exception e) {
				e.printStackTrace();
			}

		}

	}

}
